import React, { useState, useRef, useEffect } from 'react';
import TextArea from '../TextArea';
import { notification } from 'antd'
import { connect, history } from 'umi'
// import FileModal from './components/FileModal '
import './index.less';

const Ask = (props) => {
    const { loading, dispatch } = props
    const [value, setValue] = useState('')
    const [imageList, setImageList] = useState([])
    const onChange = (e) => {
        setValue(e.target.value)
    }
    const fetchData = () => {
        dispatch({
            type: 'answering/fetchListMyChatTitle',
            payload: {},
        })
    }
    const submit = () => {
        if (!value || value.replace(/[\r\n ]/g, '').length === 0) {
            return notification.warning({
                message: '温馨提示',
                description: '请输入问题后提交',
            })
        }
        dispatch({
            type: 'answering/fakeAdminStartChat',
            payload: {
                prompt: value,
                fileList: imageList
            },
            callback: (e) => {
                setValue('')
                setImageList([])
                fetchData();
                dispatch({
                    type: 'answering/save',
                    payload: {
                        aiChatData: e
                    }
                })
                history.push({
                    pathname: '/answering/answer',
                    query: {
                        ...e,
                        type: 'install'
                    }
                })
            }
        })
    }
    return <div className='AnsweringAi_box'>
        <div className='AnsweringAi_center_item'>
            <div className='AnsweringAi_title_box'>我是学管家AI助手，很高兴见到你！</div>
            <div className='AnsweringAi_desc_box'>我可以帮你写代码、读文件、写作各种创意内容，请把你的任务交给我吧~</div>
            <TextArea imageList={imageList} setImageList={setImageList} loading={loading} value={value} onOk={submit} onChange={onChange}></TextArea>
        </div>

    </div>
}

export default connect(({ loading }) => ({
    loading: loading.effects['answering/fakeAdminStartChat'],
}))(Ask);

